package cn.wolfcode.luowowo.website.controller;

import cn.wolfcode.luowowo.article.domain.Destination;
import cn.wolfcode.luowowo.article.domain.StrategyContent;
import cn.wolfcode.luowowo.article.domain.StrategyDetail;
import cn.wolfcode.luowowo.article.domain.StrategyTag;
import cn.wolfcode.luowowo.article.query.StrategyDetailQuery;
import cn.wolfcode.luowowo.article.service.IDestinationService;
import cn.wolfcode.luowowo.article.service.IStrategyCommendService;
import cn.wolfcode.luowowo.article.service.IStrategyDetailService;
import cn.wolfcode.luowowo.article.service.IStrategyTagService;
import cn.wolfcode.luowowo.cache.service.IStrategyStatisRedisService;
import cn.wolfcode.luowowo.cache.util.RedisKeys;
import cn.wolfcode.luowowo.cache.vo.StrategyStatisVO;
import cn.wolfcode.luowowo.comment.domain.StrategyComment;
import cn.wolfcode.luowowo.comment.query.StrategyCommentQuery;
import cn.wolfcode.luowowo.comment.service.IStrategyCommentService;
import cn.wolfcode.luowowo.common.util.AjaxResult;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import cn.wolfcode.luowowo.search.query.SearchQueryObject;
import cn.wolfcode.luowowo.search.service.IStrategyTemplateSearchService;
import cn.wolfcode.luowowo.search.vo.StatisVO;
import cn.wolfcode.luowowo.website.annotation.UserParam;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("strategy")
public class StrategyController {

    @Reference
    private IDestinationService destinationService;
    @Reference
    private IStrategyTagService strategyTagService;
    @Reference
    private IStrategyDetailService strategyDetailService;
    @Reference
    private IStrategyCommentService strategyCommentService;
    @Reference
    private IStrategyStatisRedisService strategyStatisRedisService;
    @Reference
    private IStrategyCommendService strategyCommendService;
    @Reference
    private IStrategyTemplateSearchService strategyTemplateSearchService;
    @RequestMapping("")
    public String index(Model model){

        //攻略推荐
        model.addAttribute("commends", strategyCommendService.getTop5());


        //1:先准备好数据zset, 再查下

        //2:查下所有zset数据
        List<StrategyStatisVO> list =strategyStatisRedisService.getAllVOByZset();
        //3:区分国内,国外排行前10
        List<StrategyStatisVO> abroadCds = new ArrayList<>();  //国外
        List<StrategyStatisVO> unabroadCds = new ArrayList<>();  //国内
        for (StrategyStatisVO vo : list) {
            if(abroadCds.size() == 10 && unabroadCds.size() == 10){
                break;
            }
            if(vo.isIsabroad()){

                if(abroadCds.size() < 10){
                    abroadCds.add(vo);
                }
            }else{
                if(unabroadCds.size() < 10){
                    unabroadCds.add(vo);
                }
            }
        }
        //4:页面共享
        model.addAttribute("abroadCds",abroadCds);
        model.addAttribute("unabroadCds",unabroadCds);



        //热门攻略查询 前 10
        List<StrategyStatisVO> hotCds  = strategyStatisRedisService.getHotTop10();
        model.addAttribute("hotCds", hotCds);

        //主题推荐: List<Map<String, Object>>

        //        //外层: list集合
        //内层: 左边:主题(id  name)  右边: 目的地集合  List集合中目的地:  (id name)
        //目前存在问题:
        //1:排序, 按主题关联到攻略的个数
        //2:只需要前面10个主题
        //3:目的地集合中数据去除重复

        List<Map<String, Object>> data = strategyTemplateSearchService.getThemeCommend();
        model.addAttribute("themeCds", data);
        //查询条件

        //国内条件: 通过省份id 跟省份名 分  : list<StatisVO>  vo(id, name, count)
        List<StatisVO> chinas = strategyTemplateSearchService.queryConditionGroup(SearchQueryObject.CONDITION_TYPE_UN_ABROAD);
        model.addAttribute("chinas", chinas);
        //国外条件: 通过国家id 国家的名 分
        List<StatisVO> abroads = strategyTemplateSearchService.queryConditionGroup(SearchQueryObject.CONDITION_TYPE_ABROAD);
        model.addAttribute("abroads", abroads);

        //主题条件: 通过主题id 主题的名 分
        List<StatisVO> themes = strategyTemplateSearchService.queryConditionGroup(SearchQueryObject.CONDITION_TYPE_THEME);
        model.addAttribute("themes", themes);
        return "strategy/index";
    }
    //首页条件查询
    @RequestMapping("/searchPage")
    public  String searchPage(Model model, @ModelAttribute("qo")SearchQueryObject qo){


        //page
        model.addAttribute("page", strategyTemplateSearchService.query(qo));

        return "strategy/searchPageTpl";
    }
    @RequestMapping("list")
    public String list(Model model, @ModelAttribute("qo")StrategyDetailQuery qo){
        //toast
        List<Destination> toasts = destinationService.getToasts(qo.getDestId());
        Destination dest = toasts.remove(toasts.size() - 1);
        model.addAttribute("toasts", toasts);
        //dest
        model.addAttribute("dest", dest);

        //tags
        List<StrategyTag> list = strategyTagService.list();
        model.addAttribute("tags", list);

        //pageInfo
        PageInfo pageInfo = strategyDetailService.query(qo);
        model.addAttribute("pageInfo", pageInfo);
        return "strategy/list";
    }

    @RequestMapping("detail")
    public String detail(Model model, Long id){
        //detail
        StrategyDetail detail = strategyDetailService.get(id);
        StrategyContent content = strategyDetailService.getContent(id);
        detail.setStrategyContent(content);

        model.addAttribute("detail",detail );

        //阅读量 + 1
        strategyStatisRedisService.viewNumIncrease(id, 1);

        //热门排行分数 + 1  : 浏览数+评论数
        strategyStatisRedisService.addHotScores(RedisKeys.STRATEGY_STATIS_VO.join(id.toString()), 1);


        model.addAttribute("vo", strategyStatisRedisService.getStrategyStatisVo(id));
        return "strategy/detail";
    }


    //攻略评论添加
    @RequestMapping("/commentAdd")
    @ResponseBody
    public Object commentAdd(StrategyComment comment, @UserParam UserInfo userInfo){

        comment.setUserId(userInfo.getId());
        comment.setUsername(userInfo.getNickname());
        comment.setCity(userInfo.getCity());
        comment.setLevel(userInfo.getLevel());
        comment.setHeadUrl(userInfo.getHeadImgUrl());
        strategyCommentService.saveOrUpdate(comment);

        //评论数 + 1
        strategyStatisRedisService.replyNumIncrease(comment.getDetailId(), 1);

        int replynum = strategyStatisRedisService.getStrategyStatisVo(comment.getDetailId()).getReplynum();

        //暂时先操作数据库, 后续使用redis实现
       /* StrategyDetail detail = strategyDetailService.get(comment.getDetailId());
        detail.setReplynum(detail.getReplynum() + 1);
        strategyDetailService.updateReplyNum(detail.getId(), detail.getReplynum());*/

        //热门排行分数 + 1  : 浏览数+评论数
        strategyStatisRedisService.addHotScores(RedisKeys.STRATEGY_STATIS_VO.join(comment.getDetailId().toString()), 1);


        return AjaxResult.SUCCESS.addData(replynum);
    }


    //评论的列表
    @RequestMapping("/comment")
    public String commentPage(Model model, @ModelAttribute("qo")StrategyCommentQuery qo){
        model.addAttribute("page",strategyCommentService.query(qo));
        return "strategy/commentTpl";
    }




    //toId : 评论的id
    //fromid: 点赞/取消点赞用户的id

    //评论的点赞
    @RequestMapping("/commentThumbUp")
    @ResponseBody
    public Object commentThumbUp(String toid, Long fromid){

        //点赞  与 取消点赞
        strategyCommentService.thumbUp(toid, fromid);

        return AjaxResult.SUCCESS;
    }


    //攻略收藏
    @RequestMapping("/favor")
    @ResponseBody
    public Object favor(Long sid, @UserParam UserInfo userInfo){

        if(userInfo == null){  //登录处理
            AjaxResult result  = new AjaxResult(false, "请先登录");
            result.setCode(102);  //102 事先约定好未登录状态码
            return result;
        }
        //收藏与取消收藏, 根据返回true/false决定
        boolean ret = strategyStatisRedisService.favor(sid, userInfo.getId());

        //排行加分
        // zadd key 分数  value
        //参数1: value , 之前分析了value就是 vo对象的在redis中key    RedisKeys.STRATEGY_STATIS_VO.join(sid.toString())
        //参数2: 分数
        strategyStatisRedisService.addCommendScores(RedisKeys.STRATEGY_STATIS_VO.join(sid.toString()), ret?1:-1);

        return new AjaxResult(ret, "").addData(strategyStatisRedisService.getStrategyStatisVo(sid));
    }



    //攻略点赞
    @RequestMapping("/strategyThumbup")
    @ResponseBody
    public Object strategyThumbup(Long sid, @UserParam UserInfo userInfo){

        if(userInfo == null){  //登录处理
            AjaxResult result  = new AjaxResult(false, "请先登录");
            result.setCode(102);  //102 事先约定好未登录状态码
            return result;
        }
        //点赞
        boolean ret = strategyStatisRedisService.thumbup(sid, userInfo.getId());

        //点赞加分
        if(ret){
            strategyStatisRedisService.addCommendScores(RedisKeys.STRATEGY_STATIS_VO.join(sid.toString()), 1);
        }

        return new AjaxResult(ret, "").addData(strategyStatisRedisService.getStrategyStatisVo(sid));
    }
    @RequestMapping("/share")
    @ResponseBody
    public Object transfer(Long sid,@UserParam UserInfo userInfo){
        if(userInfo == null){  //登录处理
            AjaxResult result  = new AjaxResult(false, "请先登录");
            result.setCode(102);  //102 事先约定好未登录状态码
            return result;
        }
        //分享功能
        strategyStatisRedisService.strategyShare(sid);
        strategyStatisRedisService.shareUp(sid, userInfo.getId());
        return AjaxResult.SUCCESS.addData(strategyStatisRedisService.getStrategyStatisVo(sid));
    }

}
